package Image;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.01.24.10.01
 */
public class Erwm1 {

    public static void main(String[] args) throws IOException, ChecksumException, NotFoundException, FormatException {
        //读取QRcode
        var img = ImageIO.read(new File("qr.png"));
        var src = new BufferedImageLuminanceSource(img);
        var binarizer = new HybridBinarizer(src);
        var binaryBitmap = new BinaryBitmap(binarizer);
        var result = new QRCodeReader().decode(binaryBitmap);
        System.out.println(result.getText());




    }




    public static void text(String[] args) throws WriterException, IOException {
        //写入QrCode1
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        var bm = new QRCodeWriter().encode("周从喜130", BarcodeFormat.QR_CODE, 200, 200, hints);
        var i = MatrixToImageWriter.toBufferedImage(bm);
        ImageIO.write(i, "png", new FileOutputStream("qr.png"));
    }
}
